<?php

/**
 * Data validation types - shopalong specific classes
 *
 * A range of DataValidator classes for use in form validation with
 * FormObserver objects.  This code is based on a concept published on DevShed
 * http://www.devshed.com/c/a/PHP/¬
 * Centralizing-the-Validation-of-Data-with-the-Observer-Pattern-in-PHP/
 * and was adapted for PHP4 by Stuart Metcalfe
 *
 * @author Stuart Metcalfe <shoopdev@googlemail.com>
 * @version 1.0
 */

/**
 * Account email validator class to validate an email address as a valid account
 */
class AccountEmailValidator extends DataValidator
{
	/**
	 * Constructor method
	 *
	 * @access public
	 * @param object $formObserver The FormObserver to contain this validator
	 */
	function AccountEmailValidator(&$formObserver)
	{
		parent::DataValidator($formObserver);
	}
	
	/**
	 * validate method for account email validator class
	 *
	 * Must be a country_id in the countries database table to be valid
	 *
	 * @access public
	 * @param string $field Name of the form field
	 * @param string $errorMsg Message to be displayed if the field isn't valid
	 * @param boolean $validOnExists Checks for existence of the account (Default true) If set to false, checks for availability of the account
	 * @return boolean Whether the field validated or not
	 */
	function validate($field,$errorMessage,$validOnExists=true)
	{
		parent::validate($field);
		$query = 'select count(*) as total from %s where customers_email_address = "%s"';
		$query = sprintf($query, TABLE_CUSTOMERS, tep_db_input($email_address));
		$checkEmailQuery = tep_db_query($query);
		$checkEmail = tep_db_fetch_array($checkEmailQuery);
		
		if ($validOnExists && $checkEmail['total'] < 1)
		{
			$this->notifyObserver($errorMessage);
			return false;
		}
		
		elseif ($validOnExists && $checkEmail['total'] > 1)
		{
			$this->notifyObserver($errorMessage);
			return false;
		}
		
		return true;
	}
}

/**
 * Country validator class to validate a form field as a country in the database
 */
class CountryValidator extends DataValidator
{
	/**
	 * Constructor method
	 *
	 * @access public
	 * @param object $formObserver The FormObserver to contain this validator
	 */
	function CountryValidator(&$formObserver)
	{
		parent::DataValidator($formObserver);
	}
	
	/**
	 * validate method for country validator class
	 *
	 * Must be a country_id in the countries database table to be valid
	 *
	 * @access public
	 * @param string $field Name of the form field
	 * @param string $errorMsg Message to be displayed if the field isn't valid
	 * @return boolean Whether the field validated or not
	 */
	function validate($field,$errorMessage)
	{
		parent::validate($field);
		
		if(!isset($this->_method[$field]) || !is_numeric($this->_method[$field]))
		{
			$this->notifyObserver($errorMessage);
			return false;
		}
		
		$checkQuery = tep_db_query('select count(*) as total from '.TABLE_COUNTRIES.' where countries_id="'.(int)$this->_method[$field].'" limit 1');
		$check = tep_db_fetch_array($checkQuery);
		$countryExists = ($check['total'] > 0);
		
		if (!$countryExists)
		{
			$this->notifyObserver($errorMessage);
			return false;
		}
		
		return true;
	}
}

/**
 * State validator class to validate a form field as a state in the database
 */
class StateValidator extends DataValidator
{
	/**
	 * Constructor method
	 *
	 * @access public
	 * @param object $formObserver The FormObserver to contain this validator
	 */
	function StateValidator(&$formObserver)
	{
		parent::DataValidator($formObserver);
	}
	
	/**
	 * validate method for state validator class
	 *
	 * Must be a country_id in the countries database table to be valid
	 *
	 * @access public
	 * @param string $field Name of the form field
	 * @param string $errorMsg Message to be displayed if the field isn't valid
	 * @return boolean Whether the field validated or not
	 */
	function validate($field,$errorMessage)
	{
		parent::validate($field);
		
		/**
		 * @todo Implement state validation
		 */
		
		return false;
		
		if(!isset($this->_method[$field]) || !is_numeric($this->_method[$field]))
		{
			$this->notifyObserver($errorMessage);
			return false;
		}
		
		$checkQuery = tep_db_query('select count(*) as total from '.TABLE_COUNTRIES.' where country_id="'.(int)$this->_method[$field].'" limit 1');
		$check = tep_db_fetch_array($checkQuery);
		$countryExists = ($check['total'] > 0);
		
		if (!$countryExists)
		{
			$this->notifyObserver($errorMessage);
			return false;
		}
		
		return true;
	}
}

?>
